<template>
	<view class="container">

		<!-- 头部轮播 -->
		<view class="carousel-section">

			<!-- 背景色区域 -->
			<view class="titleNview-background" :style="{backgroundColor:titleNViewBackground}"></view>
			<swiper class="carousel" circular @change="swiperChange">
				<swiper-item v-for="(item, index) in carouselList" :key="index" class="carousel-item">
					<image :src="item.BarImage" @click="navToDetailPage(item)" />
				</swiper-item>
			</swiper>
			<!-- 自定义swiper指示器 -->
			<view class="swiper-dots">
				<text class="num">{{swiperCurrent+1}}</text>
				<text class="sign">/</text>
				<text class="num">{{swiperLength}}</text>
			</view>
		</view>

		<!-- 屏幕先锋 -->
		<view class="f-header m-t">
			<image src="/static/temp/h1.png"></image>
			<view class="tit-box">
				<text class="tit">唯信优品-商城</text>
				<!-- <text class="tit2">Guess You Like It</text> -->
			</view>
			<!-- <text class="yticon icon-you"></text> -->
		</view>
		<view class="guess-section">
			<view v-for="(item, index) in goodsList" :key="index" class="guess-item">
				<view class="image-wrapper" @click="navToDetailPage(item)">
					<image :src="item.MainDescArr[0]" mode="aspectFit"></image>
				</view>
				<view class="tests">
					<view style="height: 120upx;line-height: 120upx;display: block;" @click="navToDetailPage(item)">
						<text class="title clamp">{{item.ProName}}</text>
					</view>
					<view style="line-height: 80upx;">
						<text class="price" style="width: 400upx;float: left;" @click="navToDetailPage(item)">￥{{item.PrePrice}}</text>
						<view @click="tocar(item)" class="yticon icon-gouwuche" style="float: left;position: relative;font-size: 50upx;margin-right: 10upx; color: #E93B3D;"></view>
					</view>
				</view>
			</view>
		</view>


	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {

		data() {
			return {
				titleNViewBackground: '',
				swiperCurrent: 0,
				swiperLength: 0,
				carouselList: [],
				goodsList: []
			}
		},
		computed: {
			...mapState(['token', 'exp_time', 'hasLogin', 'userInfo'])
		},
		onLoad(options) {
			// #ifdef H5
			// 			uni.showModal({
			// 				content: JSON.stringify(options),
			// 			})
			// 
			// 			uni.showModal({
			// 				content: options.code,
			// 			})

			if (options.state != null) {
				uni.request({
					url: this.$serverUrl + '/wx_login/' + options.state + "/" + options.code,
					header: {
						"Content-Type": "application/json",
						"Authorization": this.token
					},
					data: {},
					method: "POST",
					success: (e) => {


					},
					fail: (e) => {
						uni.showModal({
							content: "请求失败，请重试！",
							showCancel: false
						})
					}
				})

			}
			// #endif





			uni.request({
				url: this.$serverUrl + '/app/setting/GlobalNotice',
				header: {
					"Content-Type": "application/json",
					"Authorization": this.token
				},
				data: {},
				method: "POST",
				success: (e) => {

					if (e.data.value.size > 1) {
						uni.showModal({
							content: e.data.value,
							showCancel: false
						})

					}


				},
				fail: (e) => {
					uni.showModal({
						content: "请求失败，请重试！",
						showCancel: false
					})
				},
				complete: () => {
					this.loading = false;
					this.logining = false;
				}
			})

















		},
		onShow() {
			// // #ifdef H5
			// window.location.href = "https://51gsc.com/app/pmxf-ipa";
			// // #endif








			this.loadData();
		},
		methods: {
			/**
			 * 请求静态数据只是为了代码不那么乱
			 * 分次请求未作整合
			 */
			async loadData() {
				// 				let carouselList = await this.$api.json('carouselList');
				// 
				// 
				uni.request({
					url: this.$serverUrl + '/app/index/queryBanner',
					header: {
						"Content-Type": "application/json"
					},
					data: {},
					method: "POST",
					success: (e) => {

						if (e.data.success) {


							this.titleNViewBackground = e.data.rows[0].BackColor;
							this.swiperLength = e.data.rows.length;
							this.carouselList = e.data.rows;


						}
					},
					fail: (e) => {
						console.log(e.errMsg)
						return;
					}
				})


				// 				let goodsList = await this.$api.json('goodsList');
				// 				console.log("goodsList")
				// 				console.log(goodsList)
				// 
				// 
				// 				this.goodsList = goodsList || [];
				// 

				uni.request({
					url: this.$serverUrl + '/app/product/queryAll',
					header: {
						"Content-Type": "application/json"
					},
					data: {

					},
					method: "POST",
					success: (e) => {

						if (e.data.success) {

							this.goodsList = e.data.rows || [];

							if (e.statusCode !== 200) {
								// uni.showModal({
								// 	content: "网络异常",
								// 	showCancel: false
								// });
								return;
							}
						}
					},
					fail: (e) => {
						console.log(e.errMsg)
						// uni.showModal({
						// 	content: "网络异常",
						// 	showCancel: false
						// });
						return;
					}
				})


			},
			//轮播图切换修改背景色
			swiperChange(e) {
				const index = e.detail.current;
				this.swiperCurrent = index;
				this.titleNViewBackground = this.carouselList[index].background;
			},
			//详情页
			navToDetailPage(item) {
				//测试数据没有写id，用title代替

				let id = item.ProId;

				uni.navigateTo({
					url: `/pages/product/product?id=${id}`
				})
			},
			//到购物车
			tocar(item) {
				//测试数据没有写id，用title代替

				if (!this.hasLogin) {
					let url = '/pages/public/login';
					uni.navigateTo({
						url
					})
				} else {


					uni.request({
						url: this.$serverUrl + '/app/shoppingCart/put',
						header: {
							"Content-Type": "application/json",
							"Authorization": this.token
						},
						data: {
							"mem_id": this.userInfo.MemId,
							"pro_id": item.ProId,
							"pro_nums": 1

						},
						method: "POST",
						success: (e) => {
							if (e.data.success) {

								this.$api.msg(`添加成功!`);

							} else {
								console.log(e.data.message)
							}
						},
						fail: (e) => {
							console.log(e)
						}
					})


				}


















			},
		},

	}
</script>

<style lang="scss">
	page {
		background: #f5f5f5;
	}

	.m-t {
		margin-top: 16upx;
	}

	.yticon {
		font-size: 44upx;
		color: #5eba8f;
		margin-right: 16upx;
		line-height: 40upx;
	}


	/* 头部 轮播图 */
	.carousel-section {
		position: relative;
		padding-top: 10px;

		.titleNview-placing {
			height: var(--status-bar-height);
			padding-top: 44px;
			box-sizing: content-box;
		}

		.titleNview-background {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 388upx;
			transition: .4s;
		}
	}

	.carousel {
		width: 100%;
		height: 350upx;

		.carousel-item {
			width: 100%;
			height: 100%;
			padding: 0 28upx;
			overflow: hidden;
		}

		image {
			width: 100%;
			height: 100%;
			border-radius: 10upx;
		}
	}

	.swiper-dots {
		display: flex;
		position: absolute;
		left: 60upx;
		bottom: 15upx;
		width: 72upx;
		height: 36upx;
		background-image: url();
		background-size: 100% 100%;

		.num {
			width: 36upx;
			height: 36upx;
			border-radius: 50px;
			font-size: 24upx;
			color: #fff;
			text-align: center;
			line-height: 36upx;
		}

		.sign {
			position: absolute;
			top: 0;
			left: 50%;
			line-height: 36upx;
			font-size: 12upx;
			color: #fff;
			transform: translateX(-50%);
		}
	}


	.f-header {
		display: flex;
		align-items: center;
		height: 140upx;
		padding: 6upx 30upx 8upx;
		background: #fff;

		image {
			flex-shrink: 0;
			width: 80upx;
			height: 80upx;
			margin-right: 20upx;
		}

		.tit-box {
			flex: 1;
			display: flex;
			flex-direction: column;
		}

		.tit {
			font-size: $font-lg +2upx;
			color: #font-color-dark;
			line-height: 1.3;
		}

		.tit2 {
			font-size: $font-sm;
			color: $font-color-light;
		}

		.icon-you {
			font-size: $font-lg +2upx;
			color: $font-color-light;
		}
	}


	/* 猜你喜欢 */
	.guess-section {
		display: flex;
		flex-wrap: wrap;
		padding: 0 30upx;
		background: #fff;

		.guess-item {
			display: flex;
			flex-direction: row;

			padding-bottom: 40upx;

			&:nth-child(2n+1) {
				margin-right: 4%;
			}
		}


		.image-wrapper {
			width: 225upx;
			height: 225upx;
			border-radius: 3px;
			overflow: hidden;

			image {
				width: 100%;
				opacity: 1;
			}
		}

		.tests {
			margin: 10upx;
			flex-direction: column;
			width: 100%;
			font-size: 30upx;


		}


		.title {
			font-size: 35upx;
			white-space: pre-wrap;
			color: $font-color-dark;
			line-height: 1;
		}

		.price {
			// font-size: $font-lg;
			font-size: 40upx;
			color: $uni-color-primary;
			line-height: 1;
		}
	}
</style>
